<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>飞行射击游戏</title>
    <style>
        body { margin: 0; overflow: hidden; background-color: #000; }
        canvas { display: block; margin: 0 auto; background-color: #000; }
        #game-info {
            color: white;
            font-family: Arial;
            font-size: 18px;
            text-align: center;
            margin-top: 10px;
        }
    </style>
</head>
<body>
<canvas id="gameCanvas" width="400" height="600"></canvas>
<div id="game-info">分数: <span id="score">0</span> | 使用方向键移动，空格键发射</div>

<script>
    // 获取画布和上下文
    const canvas = document.getElementById('gameCanvas');
    const ctx = canvas.getContext('2d');
    const scoreElement = document.getElementById('score');

    // 游戏状态
    let score = 0;
    let gameOver = false;

    // 玩家飞机
    const player = {
        x: canvas.width / 2 - 15,
        y: canvas.height - 50,
        width: 30,
        height: 30,
        speed: 5,
        color: '#0f0',
        isMovingLeft: false,
        isMovingRight: false,
        isMovingUp: false,
        isMovingDown: false,
        isShooting: false,
        lastShootTime: 0
    };

    // 子弹数组
    let bullets = [];

    // 敌机数组
    let enemies = [];

    // 监听键盘事件
    window.addEventListener('keydown', function(e) {
        if (e.key === 'ArrowLeft') player.isMovingLeft = true;
        if (e.key === 'ArrowRight') player.isMovingRight = true;
        if (e.key === 'ArrowUp') player.isMovingUp = true;
        if (e.key === 'ArrowDown') player.isMovingDown = true;
        if (e.key === ' ') player.isShooting = true;

        // 如果游戏结束，按任意键重新开始
        if (gameOver) restartGame();
    });

    window.addEventListener('keyup', function(e) {
        if (e.key === 'ArrowLeft') player.isMovingLeft = false;
        if (e.key === 'ArrowRight') player.isMovingRight = false;
        if (e.key === 'ArrowUp') player.isMovingUp = false;
        if (e.key === 'ArrowDown') player.isMovingDown = false;
        if (e.key === ' ') player.isShooting = false;
    });

    // 更新玩家位置
    function updatePlayer() {
        if (player.isMovingLeft && player.x > 0) player.x -= player.speed;
        if (player.isMovingRight && player.x < canvas.width - player.width) player.x += player.speed;
        if (player.isMovingUp && player.y > 0) player.y -= player.speed;
        if (player.isMovingDown && player.y < canvas.height - player.height) player.y += player.speed;

        // 射击
        const currentTime = Date.now();
        if (player.isShooting && currentTime - player.lastShootTime > 300) {
            bullets.push({
                x: player.x + player.width / 2 - 2,
                y: player.y,
                width: 4,
                height: 10,
                speed: 7,
                color: '#fff'
            });
            player.lastShootTime = currentTime;
        }
    }

    // 更新子弹位置
    function updateBullets() {
        for (let i = 0; i < bullets.length; i++) {
            bullets[i].y -= bullets[i].speed;

            // 移除超出屏幕的子弹
            if (bullets[i].y + bullets[i].height < 0) {
                bullets.splice(i, 1);
                i--;
            }
        }
    }

    // 生成敌机
    function generateEnemies() {
        if (Math.random() < 0.02) {
            enemies.push({
                x: Math.random() * (canvas.width - 20),
                y: -20,
                width: 20,
                height: 20,
                speed: 1 + Math.random() * 2,
                color: '#f00'
            });
        }
    }

    // 更新敌机位置
    function updateEnemies() {
        for (let i = 0; i < enemies.length; i++) {
            enemies[i].y += enemies[i].speed;

            // 移除超出屏幕的敌机
            if (enemies[i].y > canvas.height) {
                enemies.splice(i, 1);
                i--;
            }
        }
    }

    // 检测碰撞
    function checkCollisions() {
        // 检测子弹与敌机的碰撞
        for (let i = 0; i < bullets.length; i++) {
            for (let j = 0; j < enemies.length; j++) {
                if (isColliding(bullets[i], enemies[j])) {
                    // 增加分数
                    score += 10;
                    scoreElement.textContent = score;

                    // 移除子弹和敌机
                    bullets.splice(i, 1);
                    enemies.splice(j, 1);
                    i--;
                    break;
                }
            }
        }

        // 检测玩家与敌机的碰撞
        for (let i = 0; i < enemies.length; i++) {
            if (isColliding(player, enemies[i])) {
                gameOver = true;
                break;
            }
        }
    }

    // 判断两个矩形是否碰撞
    function isColliding(rect1, rect2) {
        return rect1.x < rect2.x + rect2.width &&
            rect1.x + rect1.width > rect2.x &&
            rect1.y < rect2.y + rect2.height &&
            rect1.y + rect1.height > rect2.y;
    }

    // 绘制游戏
    function draw() {
        // 清空画布
        ctx.clearRect(0, 0, canvas.width, canvas.height);

        // 绘制玩家
        ctx.fillStyle = player.color;
        ctx.fillRect(player.x, player.y, player.width, player.height);

        // 绘制子弹
        for (const bullet of bullets) {
            ctx.fillStyle = bullet.color;
            ctx.fillRect(bullet.x, bullet.y, bullet.width, bullet.height);
        }

        // 绘制敌机
        for (const enemy of enemies) {
            ctx.fillStyle = enemy.color;
            ctx.fillRect(enemy.x, enemy.y, enemy.width, enemy.height);
        }

        // 游戏结束显示
        if (gameOver) {
            ctx.fillStyle = '#fff';
            ctx.font = '30px Arial';
            ctx.textAlign = 'center';
            ctx.fillText('游戏结束', canvas.width / 2, canvas.height / 2);
            ctx.font = '20px Arial';
            ctx.fillText('最终得分: ' + score, canvas.width / 2, canvas.height / 2 + 40);
            ctx.fillText('按任意键重新开始', canvas.width / 2, canvas.height / 2 + 80);
        }
    }

    // 重新开始游戏
    function restartGame() {
        score = 0;
        scoreElement.textContent = score;
        gameOver = false;
        player.x = canvas.width / 2 - 15;
        player.y = canvas.height - 50;
        bullets = [];
        enemies = [];
    }

    // 游戏主循环
    function gameLoop() {
        if (!gameOver) {
            updatePlayer();
            updateBullets();
            generateEnemies();
            updateEnemies();
            checkCollisions();
        }

        draw();
        requestAnimationFrame(gameLoop);
    }

    // 开始游戏
    gameLoop();
</script>
</body>
</html>